package com.xhwl.logistics.service.logistics.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.logistics.converter.mapstruct.logistics.VehicleAccessConverter;
import com.xhwl.logistics.dao.mapper.logistics.VehicleAccessMapper;
import com.xhwl.logistics.entity.logistics.VehicleAccess;
import com.xhwl.logistics.enums.logistics.DirectionEnum;
import com.xhwl.logistics.enums.logistics.PassTypeEnum;
import com.xhwl.logistics.query.VehicleAccessQuery;
import com.xhwl.logistics.service.logistics.VehicleAccessService;
import com.xhwl.logistics.util.PageResult;
import com.xhwl.logistics.util.PageResultUtils;
import com.xhwl.logistics.vo.logistics.VehicleAccessVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lin.yang
 * @since 2023-12-15
 */
@Service
@Slf4j
public class VehicleAccessServiceImpl extends ServiceImpl<VehicleAccessMapper, VehicleAccess> implements VehicleAccessService {

    @Autowired
    private VehicleAccessMapper vehicleAccessMapper;
    @Override
    public PageResult<VehicleAccessVO> pageVehicleAccess(VehicleAccessQuery query) {
        log.info("pageVehicleAccess() called with parameters => 【query = {}】",query);
        Page<VehicleAccess> vehicleAccessPage = vehicleAccessMapper.pageVehicleAccess(query, PageResultUtils.getMyBatisPlusPage(query));
        List<VehicleAccess> records = vehicleAccessPage.getRecords();
        List<VehicleAccessVO> vehicleAccessVOS = VehicleAccessConverter.INSTANCE.toVoConvertList(records);
        for (VehicleAccessVO vehicleAccessVO : vehicleAccessVOS) {
            vehicleAccessVO.setDirectionName(
                    Arrays.stream(DirectionEnum.values()).filter(x->x.getCode().equals(vehicleAccessVO.getDirection()))
                            .map(x->x.getLabel()).findFirst().orElse("")
            );

            vehicleAccessVO.setPassTypeName(
                    Arrays.stream(PassTypeEnum.values()).filter(x->x.getCode().equals(vehicleAccessVO.getPassType()))
                            .map(x->x.getLabel()).findFirst().orElse("")
            );
        }
        return PageResultUtils.getPageResult(vehicleAccessPage,vehicleAccessVOS);
    }

    @Override
    public List<VehicleAccessVO> queryVehicleAccess(VehicleAccessQuery query) {
        log.info("queryVehicleAccess() called with parameters => 【query = {}】",query);
        LambdaQueryWrapper<VehicleAccess> queryWrapper = Wrappers.lambdaQuery();
        if (ObjectUtil.isNotNull(query.getKeywords())) {
            queryWrapper.and(x->x.like(
                    ObjectUtil.isNotNull(query.getKeywords()),VehicleAccess::getVehicleOwner,query.getKeywords())
                    .or(t->t.like(ObjectUtil.isNotNull(query.getKeywords()),VehicleAccess::getPlateNo,query.getKeywords())
                            .or(z->z.like(ObjectUtil.isNotNull(query.getKeywords()),VehicleAccess::getTelephone,query.getKeywords()))
                    )
            );
        }
        List<VehicleAccess> list = Optional.ofNullable(this.getBaseMapper().selectList(queryWrapper)).orElse(Collections.emptyList());
        List<VehicleAccessVO> vehicleAccessVOS = VehicleAccessConverter.INSTANCE.toVoConvertList(list);
        for (VehicleAccessVO vehicleAccessVO : vehicleAccessVOS) {
            vehicleAccessVO.setDirectionName(
                    Arrays.stream(DirectionEnum.values()).filter(x->x.getCode().equals(vehicleAccessVO.getDirection()))
                            .map(x->x.getLabel()).findFirst().orElse("")
            );

            vehicleAccessVO.setPassTypeName(
                    Arrays.stream(PassTypeEnum.values()).filter(x->x.getCode().equals(vehicleAccessVO.getPassType()))
                            .map(x->x.getLabel()).findFirst().orElse("")
            );
        }
        return vehicleAccessVOS;
    }
}
